<template>
  <a-spin :spinning="confirmLoading">
    <a-form-model ref="form" :model="model" :rules="validatorRules">
      <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="refundDays" label="退单时效">
        <a-input-number v-model="model.refundDays" allowClear :min="0" :max="180" :formatter="limitNumber"
          :parser="limitNumber"></a-input-number> 天可退
      </a-form-model-item>
      <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="negativeSales" label="允许负库存">
        <a-radio-group v-model="model.negativeSales">
          <a-radio value="1">是</a-radio>
          <a-radio value="0">否</a-radio>
        </a-radio-group>
        <a-tooltip title="限制下单，返货，调拨负库存操作"><a-icon type="question-circle-o"></a-icon></a-tooltip>
      </a-form-model-item>

      <a-form-model-item :wrapperCol="{ span: 10, offset: 10 }">
        <a-button type="primary" htmlType="submit" @click="handleOk">保存</a-button>
      </a-form-model-item>

    </a-form-model>
  </a-spin>
</template>

<script>
import { postAction, getAction } from '@/api/manage'
import { limitNumber, isUnSignInt, isUnSignDig2 } from '@/utils/util'

export default {
  name: 'DepartMerchantSetting',
  components: {
  },
  data() {
    return {
      description: '订单设置',
      limitNumber,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 10 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 },
      },
      confirmLoading: false,
      validatorRules: {
        refundDays: [{ required: true, message: '必填项' }],
        negativeSales: [{ required: true, message: '必填项' }],
      },
      model: {},
      url: {
        save: '/sys/merchant/posMerchantSetting/save',
        query: '/sys/merchant/posMerchantSetting/queryMetchantSettings',
      },
    }
  },
  created() {
    this.querySettings()
  },
  computed: {},
  methods: {
    querySettings() {
      this.confirmLoading = true
      getAction(this.url.query, { ruleCate: 'ORDER' }).then((res) => {
        if (res.success) {
          this.assembleModel(res.result)
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.confirmLoading = false
      })
    },
    assembleModel(result) {
      console.log('===result===', result)
      if (!result) {
        result = new Array()
      }
      let ruleType10 = result.filter(item => item.ruleType === 10)
      if(ruleType10 && ruleType10.length > 0){
        this.$set(this.model, 'refundDays', ruleType10[0].ruleVal)
      }

      let ruleType11 = result.filter(item => item.ruleType === 11)
      if(ruleType11 && ruleType11.length > 0){
        this.$set(this.model, 'negativeSales', ruleType11[0].ruleVal)
      }

    },
    buildReqVo() {
      let params = new Array()
      params.push({
        ruleCate: 'ORDER',
        ruleType: 10,
        ruleVal: this.model.refundDays
      })

      params.push({
        ruleCate: 'ORDER',
        ruleType: 11,
        ruleVal: this.model.negativeSales
      })

      return { posMerchantSettingList: params }
    },
    checkParams() {
      return true
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true

          let params = this.buildReqVo()

          if (!this.checkParams(params)) {
            return false
          }

          postAction(that.url.save, params)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        } else {
          return false
        }
      })
    },
  }
}
</script>
<style scoped>
/** Button按钮间距 */
.table-page-search-wrapper {
  margin-top: -16px;
  margin-bottom: 16px;
}
</style>